<?php
require '../../common/initPage.php';
require '../../common/dbcon.php';
require '../../common/func.php';
require '../../common/showTips.php';
require '../../common/checkForm.php';

session_start();
//拦截未登录
filterLogout();

unset($_SESSION['baseInfoShowTips']);
unset($_SESSION['pwdInfoShowTips']);
unset($_SESSION['picInfoShowTips']);

//修改用户基本信息
if ($_GET['action'] == 'editBaseInfo') {
    if (checkUsername($_POST['edituname'])) {
        $editid = handledata($_POST['id']);
        $edituname = handledata($_POST['edituname']);
        $editusex = handledata($_POST['editusex']);
        $edituiden = handledata($_POST['edituiden']);
        $editudesc = handledata($_POST['editudesc']);
        if (upBaseInfo($mysqlCon, updateUserBaseInfo, $edituname, $editusex, $edituiden, $editudesc, $editid)) {
            $_SESSION['loginName'] = $edituname;
            $_SESSION['loginDesc'] = $editudesc;
            switch ($editusex) {
                case 0:
                    $_SESSION['loginSex'] = '保密';
                    break;
                case 1:
                    $_SESSION['loginSex'] = '男';
                    break;
                case 2:
                    $_SESSION['loginSex'] = '女';
            }
            switch ($edituiden) {
                case 0:
                    $_SESSION['loginIden'] = '管理员';
                    break;
                case 1:
                    $_SESSION['loginIden'] = '青铜会员';
                    break;
                case 2:
                    $_SESSION['loginIden'] = '白银会员';
                    break;
                case 3:
                    $_SESSION['loginIden'] = '钻石会员';
                    break;
                case 4:
                    $_SESSION['loginIden'] = '黑金会员';
                    break;
            }
            $_SESSION['baseInfoShowTips'][] = '修改基本信息成功';
            $_SESSION['baseInfotipsType'] = 'success';
        } else {
            $_SESSION['baseInfoShowTips'][] = '修改基本信息失败';
            $_SESSION['baseInfotipsType'] = 'danger';
        }
    } else {
        $_SESSION['tipsType'] = 'warning';
        if (!$_POST['edituname']) $_SESSION['baseInfoShowTips'][] = '用户名不能为空';
        if (!checkUsername($_POST['edituname'])) $_SESSION['baseInfoShowTips'][] = '用户名应由2~16位汉字、英文字母、数字、下划线组成';
    }
}
//修改用户密码
if ($_GET['action'] == 'editPWDInfo') {
    if (enpwd(handledata($_POST['editoldupwd']), $_SESSION['loginSalt']) == $_SESSION['loginPwd']) {
        if (!empty($_POST['editupwd'])) {
            if (checkPassword($_POST['editupwd'])) {
                if ($_POST['editupwd'] == $_POST['editcomupwd']) {
                    $salt = substr(uniqid(microtime()), -6);
                    $newPWD = enpwd(handledata($_POST['editupwd']), $salt);
                    $editid = handledata($_POST['id']);
                    if (upPWDInfo($mysqlCon, updateUserPWDInfo, $newPWD, $salt, $editid)) {
                        $_SESSION['loginPwd'] = $newPWD;
                        $_SESSION['loginSalt'] = $salt;
                        $_SESSION['pwdInfoShowTips'][] = '修改密码成功,将在下次登录生效';
                        $_SESSION['pwdInfotipsType'] = 'success';
                    } else {
                        $_SESSION['pwdInfoShowTips'][] = '修改密码失败';
                        $_SESSION['pwdInfotipsType'] = 'danger';
                    }
                } else {
                    $_SESSION['pwdInfoShowTips'][] = '两次密码输入不一致';
                    $_SESSION['pwdInfotipsType'] = 'warning';
                }
            } else {
                $_SESSION['pwdInfoShowTips'][] = '密码应由6~16位英文字母，数字，下划线组成';
                $_SESSION['pwdInfotipsType'] = 'warning';
            }
        } else {
            $_SESSION['pwdInfoShowTips'][] = '新密码不能为空';
            $_SESSION['pwdInfotipsType'] = 'warning';
        }
    } else {
        $_SESSION['pwdInfoShowTips'][] = '旧密码输入错误';
        $_SESSION['pwdInfotipsType'] = 'warning';
    }
}
//修改用户头像
if ($_GET['action'] == 'editPicInfo') {
    if (!empty($_FILES["editupic"]["name"])) {
        if (checkUserPic($_FILES["editupic"])) {
            if ($_FILES["editupic"]["error"] > 0) {
                switch ($_FILES["editupic"]['error']) {
                    case 1:
                        $_SESSION['picInfoShowTips'][] = '图片大小超过了php.ini中upload_max_filesize选项限制的值';
                        break;
                    case 2:
                        $_SESSION['picInfoShowTips'][] = '图片大小超过了表单中max_file_size选项指定的值';
                        break;
                    case 3:
                        $_SESSION['picInfoShowTips'][] = '图片只有部分被上传';
                        break;
                    case 4:
                        $_SESSION['picInfoShowTips'][] = '没有图片被上传';
                        break;
                    case 6:
                        $_SESSION['picInfoShowTips'][] = '找不到临时文件夹';
                        break;
                    case 7:
                        $_SESSION['picInfoShowTips'][] = '图片写入失败';
                        break;
                    default:
                        $_SESSION['picInfoShowTips'][] = '未知错误,上传失败';
                        break;
                }
                $_SESSION['picInfotipsType'] = 'danger';
            } else {
                $_FILES["editupic"]["name"] = get_randomFileName($_FILES["editupic"]["name"], 6);
                move_uploaded_file($_FILES["editupic"]["tmp_name"], "../../uploads/userpic/" . $_FILES["editupic"]["name"]);
                $editid = handledata($_POST['id']);
                if (upPicInfo($mysqlCon, updateUserPicInfo, $_FILES["editupic"]["name"], $editid)) {
                    unlink('../../uploads/userpic/' . $_SESSION['loginPic']);
                    $_SESSION['loginPic'] = $_FILES["editupic"]["name"];
                    $_SESSION['picInfoShowTips'][] = '修改头像成功';
                    $_SESSION['picInfotipsType'] = 'success';
                } else {
                    $_SESSION['picInfoShowTips'][] = '修改头像失败';
                    $_SESSION['picInfotipsType'] = 'danger';
                }

                $_SESSION['picInfoShowTips'][] = '上传头像成功';
                $_SESSION['picInfotipsType'] = 'success';
            }
        } else {
            $_SESSION['picInfoShowTips'][] = '非法的图片格式';
            $_SESSION['picInfotipsType'] = 'warning';
        }
    }
}

$routerUrl = '../../views/public/personInfo.html';
if($_SESSION['loginIden']=='管理员'){
    require '../../views/admin/template.html';
}else{
    require '../../views/user/template.html';
}
